import gradio as gr
import pandas as pd
import numpy as np
import plotly.graph_objects as go



css = """.gradio-slider {background-color: red}"""



class ShowAnalysis:
    def __init__(self):
        self.ratio_of_pay_decrease = 0.5
        self.ratio_of_income_increse = 0.5
        self.inflow_benchmark = 1684
        self.inflow_per_year = 451
        self.inflow_per_month = int(451 / 12)
        self.outflow_benchmark = 4612
        self.outflow_per_year = 2671
        self.outflow_per_month = int(2671 / 12)
        self.case_inflow = True
        self.case_outflow = True
    
    def need_money(self, ratio_of_pay_decrease, ratio_of_income_increse):
        m = 0
        t = ''
        # input
        v = self.inflow_benchmark * ratio_of_pay_decrease
        m += v
        decrease_per_year = self.inflow_per_year * ratio_of_pay_decrease
        decrease_per_month = self.inflow_per_month * ratio_of_pay_decrease
        t += f"考虑减少缴费，每人每年少缴费{self.inflow_per_year} * {ratio_of_pay_decrease}={int(decrease_per_year)}元 ，"
        t += f"每月少缴费【{int(decrease_per_month)}】元 ，"
        t += f"需要资金{self.inflow_benchmark} * {ratio_of_pay_decrease}={int(v)}亿元 \n"
        # output
        v = self.outflow_benchmark * ratio_of_income_increse
        m += v
        increase_per_year = self.outflow_per_year * ratio_of_income_increse
        increase_per_month = self.outflow_per_month * ratio_of_income_increse
        t += f"考虑增加支出，每人每年增加养老金{self.outflow_per_year} * {ratio_of_income_increse}={int(increase_per_year)}元 "
        t += f"每月增加养老金【{int(increase_per_month)}】元 "
        t += f"需要资金{self.outflow_benchmark} * {ratio_of_income_increse}={int(v)}亿元 \n"
        
        m = int(m)
        t += f"考虑总共需要资金{m}亿元"
        print(ratio_of_pay_decrease, ratio_of_income_increse, m)
        return m, t
    
    def need_money_from_fiscal_or_capital(self, need_money, ratio_from_fiscal, roe, div_ratio):
        cashflow_from_fiscal = int(need_money * ratio_from_fiscal)
        cashflow_from_capital = int(need_money * (1 - ratio_from_fiscal))
        capital_need = int(cashflow_from_capital / div_ratio / roe)
        total_capital = int(capital_need/0.33)
        t = f">> 所需筹集资金{need_money}亿元, 需要财政补贴{cashflow_from_fiscal}亿元，需要社保划拨资产生现金流{cashflow_from_capital}亿元\n"
        t += f">> 按照净资产收益率为{roe*100}%，股息率为{div_ratio*100}%设定，需要净资产{capital_need}亿元\n"
        t += f">> 按照净资产/总资产=0.33设定，需要总资产{total_capital}亿元\n"
        t += f">> 综上需要财政资金【{cashflow_from_fiscal}亿元】，需要社保划拨净资产【{capital_need}亿元】，或总资产【{total_capital}亿元】"
        return t
    
    def html(self):
        with gr.Blocks(css=css) as demo:
            gr.Markdown("# 社保模拟分析")
            gr.Markdown("## 基本数据")
            gr.Markdown("城乡居民养老，缴费人群37354万人，每人平均缴费451元，缴费收入1684亿元")
            gr.Markdown("城乡居民养老，领取人群17268万人，每人平均领取2671元，发放金额4612亿元，[数据来源:2023人力资源和社会保障事业发展统计公报](https://www.mohrss.gov.cn/SYrlzyhshbzb/zwgk/szrs/tjgb/202406/W020240617617024381518.pdf)")
            gr.Markdown("## 步骤一：所需资金计算")
            with gr.Row():
                ratio_of_pay_decrease = gr.Slider(0, 1, 0, step=0.01, label="缴费比例减少", interactive=True,
                                                  )
                ratio_of_income_increse = gr.Slider(0, 5, 0.905, step=0.01, label="收入比例增加", interactive=True)
            yearly_income_text = gr.Textbox(label="所需资金计算过程：", value="", interactive=False)
            with gr.Row():
                yearly_income = gr.Number(label="所需年现金流", value=1684, interactive=True)
            
            gr.Markdown("## 步骤二：资金筹集计算")
            with gr.Row():
                with gr.Column():
                    gr.Markdown("最左表示全部来源于财政补贴，最右表示全部来源于社保划拨")
                    ratio_from_fiscal = gr.Slider(
                        0, 1, 0, step=0.01, label="所需资金分解：来源于财政补贴的比例", interactive=True)
                with gr.Column():
                    gr.Markdown("净资产收益率从0.5%到8%")
                    roe = gr.Slider(0.005, 0.1, 0.066, step=0.001, label="ROE(净资产收益率)", interactive=True)
                with gr.Column():
                    gr.Markdown("分红比例从0%到100%")
                    ratio_of_dividend_payment = gr.Slider(0, 1, 0.5, step=0.01, label="分红比例", interactive=True)
            with gr.Row():
                capital_text = gr.Textbox(label="所需资本", value="", interactive=False)

            gr.on(triggers=[ratio_of_pay_decrease.change, ratio_of_income_increse.change],
                  fn=self.need_money,
                  inputs=[ratio_of_pay_decrease, ratio_of_income_increse],
                  outputs=[yearly_income, yearly_income_text])
            
            gr.on(triggers=[ratio_from_fiscal.change, roe.change, ratio_of_dividend_payment.change],
                  fn=self.need_money_from_fiscal_or_capital,
                  inputs=[yearly_income, ratio_from_fiscal, roe, ratio_of_dividend_payment],
                  outputs=[capital_text])

        return demo

pd.set_option('display.max_columns', None)
    
sd = ShowAnalysis()
sd.html().launch(server_port=8001)